package com.jerome.election.lamport;

/**
 *
 * lamport clock
 * @author Jerome
 * @date 2023/8/2
 */
public class LamportClock implements Comparable<LamportClock>{
    private int latestTime;

    public LamportClock(int latestTime) {
        this.latestTime = latestTime;
    }

    /**
     * Update clock time
     * @param requestTime
     * @return Latest clock time
     */
    public synchronized int tick(int requestTime) {
        latestTime = Integer.max(latestTime, requestTime);
        latestTime++;
        return latestTime;
    }

    @Override
    public int compareTo(LamportClock other) {
        return Integer.compare(latestTime, other.latestTime);
    }

    public int getLatestTime() {
        return latestTime;
    }
}
